﻿using System;
using System.Web.Mvc;
using Gfw.BLL.Basic.Stock;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Web.Common;
using Gfw.Web.Models.Stock;

namespace Gfw.Web.Areas.Stock.Controllers
{
    public class UserController : Controller
    {
       /// <summary>
        /// Index显示当前登录用户的股票账户基本信息
       /// </summary>
       /// <returns></returns>
       [SecurityVerify]
       [LAuthorize]
        public ActionResult Index()
       {
          var model = UsAccountLogic.GetUserAccount(L.Manage.Authentication.Context.CurrentUser.Id);
          return PartialView(model);
       }

        #region 委托买入

        [SecurityVerify]
        [LAuthorize]
        public ActionResult Buy()
        {
            var model = UsAccountLogic.GetBuyModel(L.Manage.Authentication.Context.CurrentUser.Id);
            return View(model);
        }

        [SecurityVerify]
        [LAuthorize]
        [HttpPost]
        public ActionResult Buy(BuyModel model)
        {
            try
            {
                if (model.BuyCount > model.MaxCount)
                    throw new GfwException(string.Format("帐户余额不足，你能购买的最大数量为{0}.", model.MaxCount));
                if (model.BuyCount<100 || model.BuyCount % 100 != 0)
                    throw new GfwException("购买数量只能是100的整数倍。");
                UsAccountLogic.Buy(L.Manage.Authentication.Context.CurrentUser.Id, model.BuyCount, model.CurrentPrice);
              return  RedirectToAction("Index");
            }
            catch(Exception exp)
            {
                ModelState.AddModelError("", exp.Message);
                return View(model);
            }
        }

        #endregion

        #region 委托卖入

        [SecurityVerify]
        [LAuthorize]
        public ActionResult Sell()
        {
            
            var model = UsAccountLogic.GetSellModel(L.Manage.Authentication.Context.CurrentUser.Id);
            return View(model);
        }

        [SecurityVerify]
        [LAuthorize]
        [HttpPost]
        public ActionResult Sell(SellModel model)
        {
            try
            {
                if (model.SellCount < 100 || model.SellCount % 100 != 0)
                {
                    throw new Exception("卖出数量必须是100的整数倍");
                }
                if (model.SellCount > model.MaxCount)
                    throw new GfwException(string.Format("帐户余额不足，卖出数量不能大于{0}", model.MaxCount));
                UsAccountLogic.Sell(L.Manage.Authentication.Context.CurrentUser.Id, model.SellCount, model.CurrentPrice);
                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ModelState.AddModelError("", exp.Message);
                return View(model);
            }
        }
        #endregion

        #region 撤单

        [SecurityVerify]
        [LAuthorize]
        [OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
        public ActionResult Cancel()
        {
            if (!StockManager.ServcieIsRunning())
            {
                ViewBag.CanCancel = false ;
                return View();
            }
            else
            {
                ViewBag.CanCancel = true;
                var list = UsAccountLogic.GetCanCancelAgencyModelList(L.Manage.Authentication.Context.CurrentUser.Id);
                return View(list);
            }
            
        }

        [SecurityVerify]
        [LAuthorize]
        public ActionResult CancelTrade(string id)
        {
            UsAccountLogic.CancelTrade(id);
            ViewBag.Done = true;
            return PartialView();
        }

        #endregion

        #region 交易查询

        /// <summary>
        /// 当前选择的页数
        /// </summary>
        /// 如果是选择参数，也可以这样处理
        int PageIndex
        {
            get { return Session["BargainQuery_PageIndex"].ConvertTo<int>(1); }
            set { Session["BargainQuery_PageIndex"] = value; }
        }

        private BargainQueryModel QueryModel
        {
            get { return Session["BargainQuery_BargainQueryModel"].ConvertTo<BargainQueryModel>(null); }
            set { Session["BargainQuery_BargainQueryModel"] = value; }
        }

        [SecurityVerify]
        [LAuthorize]
        public ActionResult BargainQuery(BargainQueryModel model)
        {
            QueryModel = model;
            PageIndex = 1;
            return View(model);
        }

        [LAuthorize]
        public PartialViewResult BargainQueryList(int? page)
        {

            int totalSize;
            int index = page ?? PageIndex;
            if (index < 1) index = 1;

            var list = UsAccountLogic.BargainReocrdQuery(L.Manage.Authentication.Context.CurrentUser.Id, QueryModel.Start, QueryModel.End, index - 1, ViewHelper.PageSize, out totalSize);
            ViewBag.TotalSize = totalSize;
            PageIndex = ViewBag.PageIndex = index;

            return PartialView(list);
        }

        #endregion

    }
}
